from typing import Tuple, Any
from PIL import Image
from PIL import ImageFilter
import matplotlib.pyplot as plt

class ImageProcessor():
    def __init__(self,image,*args):
        self.image=image
        self.args=args
    def process(self):
        pass

class ImageProcessor_Grayscale(ImageProcessor):
    def __init__(self, image, *args):
        super(ImageProcessor_Grayscale,self).__init__(image, *args)
    def process(self):
        self.image=self.image.convert('L')
        return self.image

class ImageProcessor_resize(ImageProcessor):
    def __init__(self, image, *args):
        super(ImageProcessor_resize,self).__init__(image, *args)
    def process(self):
        self.image=self.image.resize(size=self.args)
        return self.image

class ImageProcessor_dim(ImageProcessor):
    def __init__(self, image, *args):
        super(ImageProcessor_dim,self).__init__(image, *args)
    def process(self):
        self.image=self.image.filter(ImageFilter.BLUR)
        return self.image

class ImageProcessor_EdgeExtraction(ImageProcessor):
    def __init__(self, image, *args):
        super(ImageProcessor_EdgeExtraction,self).__init__(image, *args)
    def process(self):
        self.image=self.image.filter(ImageFilter.CONTOUR)
        return self.image

class ImageShop():
    def __init__(self,format,path,ImgList,processed,processing_img):
        self.format=format
        self.path=path
        self.ImgList=ImgList
        self.processed=processed
        self.processing_img=processing_img
    def load_images(self):
        self.processing_img=Image.open(self.path)
        return self.processing_img
    def __batch_ps(self,Processor=1):
        self.Processor=Processor
        #有四种处理方式，1表示灰度，2表示改变大小(此处缩放为原来的1/4)，3表示模糊操作，4表示边缘提取。
        #默认为1
        if self.Processor ==2:
            lst=self.processing_img.size
            return ImageProcessor_resize(self.processing_img,lst[0]//4,lst[1]//4).process()
        elif self.Processor ==3:
            return ImageProcessor_dim(self.processing_img).process()
        elif self.Processor ==4:
            return ImageProcessor_EdgeExtraction(self.processing_img).process()
        elif self.Processor==0:
            self.processing_img.save('imgg.png')
        elif self.Processor==5:
            self.processing_img.show()
        else:
            return ImageProcessor_Grayscale(self.processing_img).process()
    def batch_ps(self,*args):
        #args参数列表接收1,2,3,4四个参数，表示对该图片轮流进行不同操作。
        #1,2,3,4参数所表示的操作同__batch_ps()
        self.args=args
        for i in args:
            self.processing_img=self.__batch_ps(Processor=int(i))
        return self.processing_img
    def save(self):
        self.processing_img.save('imgg.png')
        # om.s

class TestImageShop():
    def __init__(self,format,path,ImgList,imgg,img_lst,processed_lst,*operator_list):
        self.operator_list=operator_list
        self.format=format
        self.path=path
        self.ImgList=ImgList
        self.imgg=imgg
        self.img_lst=img_lst
        self.processed_lst=processed_lst
    def TestMake(self):
        global imgg
        operator_list_name=['存储','灰度','改变大小','模糊','边缘提取','显示']
        for operator in self.operator_list:
            print("执行操作"+operator_list_name[int(operator)])
            self.imgg=ImageShop(format='png', path=self.path, ImgList=self.img_lst, processed=self.processed_lst,processing_img=self.imgg).batch_ps(int(operator))

def main():
    path='C:/Users/Administrator/Desktop/using_python/using_python/lx.jpg'
    img_lst=[]
    processed_lst=[]
    imgg=Image.open(path)
    print("测试TestImageShop:")
    TestImageShop('png',path,img_lst,imgg,img_lst,processed_lst,1,2,3,4,5).TestMake()

if __name__ =='__main__':
    main()